package com.droidbank.util;

public class FormatDecimal {
	
	
	//------------------------------------------------------------------------
	
	public static String formatString(int integers, int decimals, String num, boolean zero4Pad)
	throws NumberFormatException
	{
		@SuppressWarnings("unused")
		boolean negative = false;
		
		if(integers < 0 || decimals < 0)
		{
			return new String("0");
		}
		
		StringBuffer sb = new StringBuffer(integers + decimals + 1);
		int i=0;
		for(i =0; i < num.length(); i++)
		{
			if(num.charAt(i) == '.')
			{
				break;
			}
			if(num.charAt(i) == '-')
			{
				negative = true;
			}
			
		}
		
		StringBuffer b = new StringBuffer();
		for(int j =i-1; j >= 0; j--)
		{
		
				if(Character.isDigit(num.charAt(j)))
				{
				b.append(num.charAt(j));	
				}
				else if(j ==0 && num.charAt(j) == '-')
				{
					b.append(num.charAt(j));
					break;
				}
				else
					throw new NumberFormatException("Can parse String to number");
		}
		
		for(int k = integers-1; k >=0; k--)
		{
			if(k > b.length()-1)
			{
				if(zero4Pad){
				sb.append("0");
				}
				else
					sb.append(" ");
				continue;
			}
			else
				sb.append(b.charAt(k));
		}
		
		
		sb.append(".");
		
		int u=0;
		for(int p = i+1; u <  decimals && p < num.length(); u++, p++)
		{
			if(Character.isDigit(num.charAt(p)))
			{
			sb.append(num.charAt(p));	
			}
			else
				throw new NumberFormatException("Can parse String to number");
		}
		
		
		return sb.toString();
	}
	
	//------------------------------------------------------------------------	
	
	//------------------------------------------------------------------------
	
	
	public static double format(int decimals, double number) 
	{	
		if(decimals < 0)
		{
			return 0;
		}
		
		return Math.floor((number * Math.pow(10, decimals)) + .5)/Math.pow(10, decimals);
		
		
	}
	
	
	//------------------------------------------------------------------------
	
	public static double format(int integers, int decimals, double number) 
	{
		boolean negative = false;
		
		if(integers < 0 || decimals < 0)
		{
			return 0;
		}
		
		String num = new Float(number).toString();
		
		
		StringBuffer sb = new StringBuffer(integers + decimals + 1);
		int i=0;
		for(i =0; i < num.length(); i++)
		{
			if(num.charAt(i) == '.')
			{
				break;
			}
			
			if(num.charAt(i) == '-')
			{
				negative = true;
			}
			
			
		}
		
		StringBuffer b = new StringBuffer();
		for(int j =i-1; j >= 0; j--)
		{
		
				if(Character.isDigit(num.charAt(j)))
				{
				b.append(num.charAt(j));	
				}
				
		}
		
		for(int k = integers-1; k >=0; k--)
		{
			if(k > b.length()-1)
			{
				sb.append("0");
				continue;
			}
			else
				sb.append(b.charAt(k));
		}
		
		
		sb.append(".");
		
		int u=0;
		for(int p = i+1; u <  decimals && p < num.length(); u++, p++)
		{
			if(Character.isDigit(num.charAt(p)))
			{
			sb.append(num.charAt(p));	
			}
		}
		
		
		return new Double(sb.toString()).floatValue() * ((negative)? -1: 1);
	}
	
	//------------------------------------------------------------------------
	
	public static class StringFormatException extends Exception
	{
	
		private static final long serialVersionUID = 1L;

		public StringFormatException(String s)
		{
			super(s);
		}
	}
	
	

}

